<?php

namespace app\admin\controller\store;

use app\common\controller\Backend;
use app\common\model\Technician;
use app\common\model\TechnicianServe;
use http\Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 店铺
 *
 * @icon fa fa-circle-o
 */
class Store extends Backend
{

    /**
     * Store模型对象
     * @var \app\admin\model\store\Store
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\store\Store;
        $this->view->assign("isCheckList", $this->model->getIsCheckList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function add()
{
  if (false === $this->request->isPost()) {
    return $this->view->fetch();
  }
//        if(!$params){
//            $this->error('提交错误');
//        }
  $params = $this->request->post('row/a');
     
  if (empty($params)) {
    $this->error(__('Parameter %s can not be empty', ''));
  }
  $uid =$params['uid'];
  $name= $params['name'];
  $image= $params['image'];
  $homediv= $params['homediv'];
  $code=rand(111111,999999);
  $store_address= $params['store_address'];
  $hours_start= $params['hours_start'];
      $hours_end= $params['hours_end'];
      $serve_image= $params['serve_image'];
      $serve_phone= $params['serve_phone'];
      $store_introduction= $params['store_introduction'];
      $store_recruit= $params['store_recruit'];
      $scope= $params['scope'];
//      $lincense= $params['lincense'];
      $lon= $params['lon'];
      $lat= $params['lat'];
      $phone= $params['phone'];
  $data=[
         'uid'=>$uid,
         'name'=>$name,
         'image'=>$image,
         'homediv'=>$homediv,
         'store_code'=>$code,
         'store_address'=>$store_address,
          'hours_start'=>$hours_start,
          'hours_end'=>$hours_end,
         'serve_image'=>$serve_image,
        'serve_phone'=>$serve_phone,
        'store_introduction'=>$store_introduction,
       'store_recruit'=>$store_recruit,
       'scope'=>$scope,
//      'lincense'=>$lincense,
     'lon'=>$lon,
     'lat'=>$lat,
     'phone'=>$phone
  ];
  $params = $this->preExcludeFields($params);

  if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
    $params[$this->dataLimitField] = $this->auth->id;
  }
  $result = false;
  Db::startTrans();
  try {
    //是否采用模型验证
    if ($this->modelValidate) {
      $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
      $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
      $this->model->validateFailException()->validate($validate);
    }
    $result = $this->model->allowField(true)->create($data);
    Db::commit();
  } catch (ValidateException|PDOException|Exception $e) {
    Db::rollback();
    $this->error($e->getMessage());
  }
  if ($result === false) {
    $this->error(__('No rows were inserted'));
  }
  $this->success();
}

  /**
   * 删除
   */
  public function del($ids = "")
  {
    if (!$this->request->isPost()) {
      $this->error(__("Invalid parameters"));
    }
    $ids = $ids ? $ids : $this->request->post("ids");
    if ($ids) {
      $pk = $this->model->getPk();
      $adminIds = $this->getDataLimitAdminIds();
      if (is_array($adminIds)) {
        $this->model->where($this->dataLimitField, 'in', $adminIds);
      }
      $list = $this->model->where($pk, 'in', $ids)->select();

      $count = 0;
      foreach ($list as $k => $v) {
        $count += $v->delete();
        \app\admin\model\store\Serve::destroy(['store_id' => $v->store_id]);
        Technician::destroy(['store_id' => $v->store_id]);
        TechnicianServe::destroy(['store_id' => $v->store_id]);
      }
      Db::startTrans();
      try {
        foreach ($list as $k => $v) {
          $count += $v->delete();
          \app\common\model\Serve::destroy(['store_id' => $v->store_id]);
          Technician::destroy(['store_id' => $v->store_id]);
          TechnicianServe::destroy(['store_id' => $v->store_id]);
        }
        Db::commit();
      } catch (PDOException $e) {
        Db::rollback();
        $this->error($e->getMessage());
      } catch (Exception $e) {
        Db::rollback();
        $this->error($e->getMessage());
      }
      if ($count) {
        $this->success();
      } else {
        $this->error(__('No rows were deleted'));
      }
    }
    $this->error(__('Parameter %s can not be empty', 'ids'));
  }



}
